define(["jquery", "bootstrap", "backend", "table", "form"], function ($, undefined, Backend, Table, Form) {
  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "fssy/departmentmetrics/index" + location.search,
          add_url: "fssy/departmentmetrics/add",
          edit_url: "fssy/departmentmetrics/edit",
          del_url: "fssy/departmentmetrics/del",
          multi_url: "fssy/departmentmetrics/multi",
          import_url: "fssy/departmentmetrics/import",
          table: "fssy_departmentmetrics",
        },
      });

      var table = $("#table");

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "id",
        fixedColumns: true,
        fixedRightNumber: 1,
        columns: [
          [
            { checkbox: true },
            { field: "id", title: __("Id") },
            { field: "department.department", title: __("科室"), formatter: Table.api.formatter.label },
            {
              field: "operator",
              title: __("Operator"),
              operate: "LIKE",
              cellStyle: function (value, row, index) {
                return { css: { color: "#FF790D", "font-weight": "bold" } };
              },
            },
            { field: "beds", title: __("Beds") },
            { field: "total_nurses_start", title: __("Total_nurses_start") },
            { field: "total_nurses_end", title: __("Total_nurses_end") },
            { field: "ward_nurses_start", title: __("Ward_nurses_start") },
            { field: "ward_nurses_end", title: __("Ward_nurses_end") },
            { field: "nurse_hours", title: __("Nurse_hours") },
            { field: "catheter_count", title: __("Catheter_count") },
            { field: "gastric_count", title: __("Gastric_count") },
            { field: "central_catheter_coun", title: __("Central_catheter_coun") },
            { field: "tracheal_count", title: __("Tracheal_count") },
            { field: "vap_count", title: __("Vap_count") },
            { field: "clabsi_count", title: __("Clabsi_count") },
            { field: "cauti_count", title: __("Cauti_count") },
            { field: "vent_days", title: __("Vent_days") },
            { field: "restraint_days", title: __("Restraint_days") },
            { field: "stage2_ulcer", title: __("Stage2_ulcer") },
            { field: "stage1_ulcer", title: __("Stage1_ulcer") },
            { field: "falls_count", title: __("Falls_count") },
            { field: "falls_severity1", title: __("Falls_severity1") },
            { field: "falls_severity2", title: __("Falls_severity2") },
            { field: "falls_severity3", title: __("Falls_severity3") },
            { field: "falls_death", title: __("Falls_death") },
            { field: "falls_injury", title: __("Falls_injury") },
            {
              field: "createtime",
              title: __("Createtime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "updatetime",
              title: __("Updatetime"),
              operate: "RANGE",
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
              sortable: true,
            },
            { field: "operate", title: __("Operate"), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate },
          ],
        ],
      });

      // 为表格绑定事件
      Table.api.bindevent(table);
    },

    add: function () {
      $(document).ready(function () {
        // console.log("#c-operator = ", $("#c-operator")); // 检查是否存在
        // // 添加时获取当前用户名 赋值给'操作员' =>  Add
        // $.ajax({
        //   url: "/TcQozAZEFm.php/fssy/Departmentmetrics/currentUser",
        //   method: "GET",
        //   success: function (data) {
        //     console.log("成功 = ", data);
        //     $("#c-operator").val(data.user);
        //   },
        //   error: function (xhr, status, error) {
        //     console.error("Error:", error);
        //   },
        // });
      });

      Controller.api.bindevent();
    },

    edit: function () {
      Controller.api.bindevent();
    },

    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };

  return Controller;
});
